package com.ktgj.pay.service.impl;

import com.ktgj.pay.domain.InvoiceChecking;
import com.ktgj.pay.mapper.InvoiceCheckingMapper;
import com.ktgj.pay.phoneSound.Console;
import com.ktgj.pay.phoneSound.SimpleXmlImpl;
import com.ktgj.pay.phoneSound.TransactionClient;
import com.ktgj.pay.service.IInvoiceCheckingService;
import com.ktgj.pay.service.IPhoneSoundPayService;
import com.ktgj.pay.utils.Toolkit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 发票核验Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-24
 */
@Service
public class PhoneSoundPayServiceImpl implements IPhoneSoundPayService
{

    @Override //手机号， 银行卡号， 身份证号， 银行开户人姓名， 支付金额
    public String voicePayment(String merOrderNo, String mobileNumber, String bankCardNo, String idCardNo, String idCardName, String orderAmount, String asynAddress) {
        try {
            String url = "https://dnaserver.payeco.com/services/ApiV2ServerRSA";  //生产环境
//            String url = "http://58.248.38.252:9080/services/ApiV2ServerRSA";//外网
//            String url = "https://test.payeco.com:9443/services/ApiV2ServerRSA";//外网
//            String url = "http://10.123.18.64:9080/services/ApiV2ServerRSA";//外网
            TransactionClient target = new TransactionClient(url);
            target.setServerCert(Toolkit.getPropertyFromFile(Toolkit.GDYILIAN_CERT_PUB_64));
            //交易密钥, 随机生成, 用于加密解密报文
            String encryptKey = Toolkit.random(24);

            //交易结果CA证书异步返回测试地址， 请修改为商户服务器提供的地址, 类型＋地址, 请参照<<银联语音支付平台接口规范>>
            String returnUrl = "http://商户异步返回地址";

            target.setMerchantNo("1202020000159"); //商户类型+商户编号
            target.setMerchantPwd("26ECD8A767B24D29"); //商户Mac密钥
            target.setTerminalNo("02064455"); //商户终端编号


            //是否即时支付,一线通选非即时支付
            boolean payNow = true;

            //测试持卡人信息, 请修改为商户测试人员的信息
//            String mobileNumber = "18745654672";

//            String bankCardNo = "6212260913001854847"; //借记卡
            String pin="";


            String idCardType = "01"; //银行开户证件类型，　01:身份证，02:护照，03:军人证，04:台胞证
//            String idCardNo = "231182200010260536"; //银行开户证件号
//            String idCardName = "李博洋"; //银行开户姓名
            String userName = ""; //订单商品受益人，多个受益人以","分割．
            String bankAddress = ""; //银行开户地，信用卡可填空，省市以","号分割．
            String ipAddress = "119.131.176.39"; //持卡人登录IP地址．
            String idCardAddress = ""; //身份证地址,截取至街道, 特殊风控.
            String productPhoneNumber = ""; //受益手机号，电话充值商户需填写;
            String productAddress = ""; //商品销售地，省市以","号分割，团购商户需填写;
            String bankPhoneNumber = "";
            String extTransData = ""; //额外交易数据

            //订单信息，金额，描述，备注，订单号。
//            String orderAmount = "10";
            String currency = "CNY";
            String orderDescription = "挽救支付宝订单";
            String remark = "";
//            String merOrderNo = merOrderNo;
            String synAddress = "http://www.baidu.com";
//            String asynAddress= "http://106.38.39.137:8080/flight/callback/phoneSoundPayCallback";
            String smsCode = "";
            String userFlag = "";
            String password = "";

            //新用户业务交换数据
            String transDataNewUser = "54654a654werew5847848";

            SimpleXmlImpl result = null;
            Console.WriteLine("输入Enter继续连接测试:\r\n");
//            Console.ReadLine();
            result = (SimpleXmlImpl)target.connectionTest(Console.GetSerialNo(), encryptKey);
            Console.WriteLine(result);
            Console.WriteLine("输入Enter继续持卡人银行卡信息查询:\r\n");
//            Console.ReadLine();
            result = (SimpleXmlImpl)target.accountQuery(Console.GetSerialNo(), bankCardNo, "", mobileNumber, "1", currency, idCardName, idCardNo, bankAddress, idCardType, userName,
                    productPhoneNumber, transDataNewUser, "", "", ipAddress, "", "", idCardAddress, bankPhoneNumber, "", smsCode, encryptKey);
            String bankName = result.getBankName();
            Console.WriteLine(result);
            Console.WriteLine("输入Enter继续即时支付:\r\n");
//            Console.ReadLine();
            if (Float.parseFloat(orderAmount) > Float.parseFloat(result.getAmount()))
            {
                Console.WriteLine("该银行卡超过当日交易金额上限，请明天再交易\r\n");
                return "该银行卡超过当日交易金额上限，请明天再交易";
//                return;
            }
            if (result.getRespCode().equals("T436"))
            {
                Console.WriteLine("该银行卡交易时间受限，请明天八点以后再交易\r\n");
                return "该银行卡交易时间受限，请明天八点以后再交易";
//                return;
            }
            if (result.getRespCode().equals("T432"))
            {
                Console.WriteLine("银行卡被列入黑名单，拒绝交易；\r\n");
                return "银行卡被列入黑名单，拒绝交易";
//                return;
            }
            if (result.getRespCode().equals("T404"))
            {
                Console.WriteLine("系统不支持的银行卡；\r\n");
                return "系统不支持的银行卡";
//                return;
            }
            if (result.getRespCode().equals("0000"))//0000：系统受信任卡，无需提供持卡人信息；
            {
                result = (SimpleXmlImpl)target.pay(Console.GetSerialNo(), bankCardNo, "", mobileNumber, orderAmount, currency, synAddress, asynAddress, "",
                        merOrderNo, "02", "", orderDescription,remark, "00", idCardName, idCardNo, bankAddress, idCardType, userName, productPhoneNumber,
                        transDataNewUser, "", "", ipAddress, "", userFlag, idCardAddress, bankPhoneNumber, "", smsCode, "", password, encryptKey);
            }
            else
            {
                //T438：系统交易过的新卡，需要提供持卡人信息；；T437：系统未交易过的新卡，需要提供持卡人信息；
                //如果订单类型要求本人消费，或开户证件非身份证则必须提供受益人姓名，且与持卡人姓名相同。
                //开户姓名+“|”+开户证件号码+“|”+开户银行所在省市+“|”+开户证件号码类型（参照7.2）+“|”
                //+收益人姓名（如乘机人姓名，本人消费时与开户姓名一致）
                result = (SimpleXmlImpl)target.pay(Console.GetSerialNo(), bankCardNo, "", mobileNumber, orderAmount, currency, synAddress, asynAddress, "",
                        merOrderNo, "02", "", orderDescription,remark, "00", idCardName, idCardNo, bankAddress, idCardType, userName, productPhoneNumber,
                        transDataNewUser, "", "", ipAddress, "", userFlag, idCardAddress, bankPhoneNumber, "", smsCode, "", password, encryptKey);
            }
            Console.WriteLine(result);
            return "请注意来电," + result.getOrganisationNo();
//            Console.WriteLine("输入Enter继续商户订单查询:\r\n");
//            Console.ReadLine();
//            String orderNo = result.getOrderNo();
//            result = (SimpleXmlImpl)target.orderQuery(Console.GetSerialNo(), bankCardNo, "01", mobileNumber, orderNo, merOrderNo, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter继续缴费冲正:\r\n");
//            Console.ReadLine();
//            String reference = "";
//            result = (SimpleXmlImpl)target.orderQuash(Console.GetSerialNo(), bankCardNo, "01", mobileNumber, orderAmount, currency, merOrderNo, orderNo, reference, remark, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter继续订单查询:\r\n");
//            Console.ReadLine();
//            result = (SimpleXmlImpl)target.orderQuery(Console.GetSerialNo(), bankCardNo, "01", mobileNumber, result.getOrderNo(), merOrderNo, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter继续调帐:\r\n");
//            Console.ReadLine();
//            result = (SimpleXmlImpl)target.adjustApply(Console.GetSerialNo(), bankCardNo, orderAmount, currency, idCardName, reference, remark, orderNo, bankName, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter继续调帐查询:\r\n");
//            Console.ReadLine();
//            result = (SimpleXmlImpl)target.adjustQuery(result.getAcqSsn(), bankCardNo, orderAmount, currency, idCardName, reference, remark, result.getTransDatetime(), bankName, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter继续退款:\r\n");
//            Console.ReadLine();
//            result = (SimpleXmlImpl)target.orderRefund(Console.GetSerialNo(), bankCardNo, "01", mobileNumber, orderAmount, currency, merOrderNo, orderNo, reference, remark, encryptKey);
//            Console.WriteLine(result);
//            Console.WriteLine("输入Enter进行订单挽救:\r\n");
//            Console.ReadLine();
//            String deliveryAddress = "";
//            String system3rdName = "";
//            String deviceData = "";
//            boolean mobileChecked = true;
//            int merchantAccountLevel = 1;
//            String location = "";
//            int merchantAccountActivity=1;
//            boolean vip = true;
//            String transData = "";
//            RescueXmlImpl result2 = (RescueXmlImpl)target.rescueOrder(Console.GetSerialNo(), bankCardNo, mobileNumber, orderAmount, currency, merOrderNo, orderDescription, synAddress ,
//                    asynAddress, "", "02", "", "00", reference, idCardName, idCardNo, bankAddress, idCardType, userName,
//                    productPhoneNumber, deviceData, deliveryAddress, ipAddress, location, system3rdName, mobileChecked, merchantAccountLevel, merchantAccountActivity,
//                    vip, transData, userFlag, encryptKey);
//            Console.WriteLine(result2);
        } catch (Exception e) {
            return "付款失败";
            // TODO: handle exception
        }
    }
}
